<div class="container-fluid">

    <div class="wrapper">
        <div class="panel" id="nav">
            <div class="panel-heading"><h3 class="panel-title">新建入库单-手动录入</h3></div>
            <div class="panel-body">

                <form method="post" id="myform" class="form-horizontal" enctype="multipart/form-data">
                    <div class="row">
                        <div class="form-group">
                            <label class="col-md-3 control-label required">选择入库原因</label>

                            <div class="col-md-2">
                                <select class="form-control" name="type">
                                    <option value="">--请选择--</option>
                                    <volist name="stock_in_type" id="item">
                                        <option value="{$key}">{$item}</option>
                                    </volist>
                                </select>
                            </div>

                            <label class="col-md-2 control-label required">SKU</label>

                            <div class="col-md-3">
                                <input type="text" class="form-control" name="sku"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-md-3 control-label required">出口退税</label>

                            <div class="col-md-2">
                                <select class="form-control" name="export_tax_rebate">
                                    <option value="">--请选择--</option>
                                    <volist name="export_tax_rebate" id="item">
                                        <option value="{$key}">{$item}</option>
                                    </volist>
                                </select>
                            </div>
                            <label class="col-md-2 control-label required">SKU名称</label>

                            <div class="col-md-3">
                                <input type="text" class="form-control" name="sku_name"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-md-3 control-label required">商业主体</label>

                            <div class="col-md-2">
                                <select class="form-control" name="enterprise_dominant">
                                    <option value="">--请选择--</option>
                                    <volist name="enterprise_dominant" id="item">
                                        <option value="{$key}">{$item}</option>
                                    </volist>
                                </select>
                            </div>
                            <label class="col-md-2 control-label required">SKU储位</label>

                            <div class="col-md-3">
                                <input type="text" class="form-control" name="storage_position"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-md-3 control-label required">供应商</label>

                            <div class="col-md-2">
                                <select class="form-control" name="supplier_id" id="supplier_id">
                                </select>
                            </div>
                            <label class="col-md-2 control-label required">单价</label>

                            <div class="col-md-3">
                                <input type="text" class="form-control" name="price" onchange="count_money()"/></div>
                        </div>

                        <div class="form-group">
                            <label class="col-md-3 control-label required">中转仓</label>

                            <div class="col-md-2">
                                <select class="form-control" name="transfer_hopper_id">
                                    <option value="">--请选择--</option>
                                    <volist name="transfer_hopper_id" id="item">
                                        <option value="{$key}">{$item}</option>
                                    </volist>
                                </select>
                            </div>
                            <label class="col-md-2 control-label required">数量</label>

                            <div class="col-md-2">
                                <input type="text" class="form-control" name="num" onchange="count_money()"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-md-3 control-label required">运输方式</label>

                            <div class="col-md-2">
                                <select class="form-control" name="transfer_type">
                                    <option value="">--请选择--</option>
                                    <volist name="transfer_type" id="item">
                                        <option value="{$key}">{$item}</option>
                                    </volist>
                                </select>
                            </div>
                            <label class="col-md-2 control-label required">金额</label>

                            <div class="col-md-2">
                                <input type="text" class="form-control" name="money" onchange="count_money()"/>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-md-3 control-label required">成本</label>

                            <div class="col-md-2">
                                <input type="text" class="form-control" name="cost" value="0" onchange="count_money()"/>
                            </div>
                            <label class="col-md-2 control-label required">运费</label>

                            <div class="col-md-3">
                                <input type="text" class="form-control" name="transporttation_expense" value="0"
                                       onchange="count_money()"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-md-3 control-label required">入库时间</label>

                            <div class="col-md-2">
                                <input type="text" class="form-control form-datetime" name="op_time"/>
                            </div>
                            <label class="col-md-2 control-label">备注</label>

                            <div class="col-md-3">
                                <input type="text" class="form-control" name="remark"/></div>
                        </div>
                        <div class="form-group">
                            <label class="col-md-3 control-label required">站点</label>

                            <div class="col-md-2">
                                <select class="form-control" name="site_id">
                                    <option value="">--请选择--</option>
                                    <volist name="site_id" id="item">
                                        <option value="{$key}">{$item}</option>
                                    </volist>
                                </select>
                            </div>
                        </div>

                    </div>
                    <div class="form-group">
                        <div class="col-md-3 col-md-offset-5">
                            <button class="btn btn-primary" type="button" id="submit">提交</button>
                            <button class="btn btn-white" type="reset">重置</button>
                        </div>
                    </div>

                </form>

                <div class="show" style="display: none">
                    <table class="table table-striped  table-hover" cellspacing="0" width="100%">
                        <thead>
                        <tr>
                            <th>入库单</th>
                            <th>站点</th>
                            <th>储位号</th>
                            <th>主体</th>
                            <th>中转仓</th>
                            <th>出口退税</th>
                            <th>采购单</th>
                            <th>质检单</th>
                            <th>入库日期</th>
                            <th>sku</th>
                            <th>sku中文名称</th>
                            <th>供应商</th>
                            <th>数量</th>
                            <th>价格</th>
                            <th>金额</th>
                            <th>运费</th>
                            <th>成本</th>
                            <th>入库类型</th>
                            <th>目的仓</th>
                            <th>运输方式</th>
                            <th>备注</th>
                            <th>入库人</th>
                        </tr>
                        </thead>
                        <tbody class="show_detail">
                        </tbody>
                    </table>
                </div>
                <div class="clearfix"></div>
                <hr>
                <h3>特殊入库批量上传数据：</h3>
                <form id="upload_form" name="form1" enctype="multipart/form-data" method="post">
                    <div class="form-group">
                        <label class="col-md-3 control-label required">批量上传</label>
                        <div class="col-md-3">
                            <input type="file" name="file" class="tx1">
                        </div>
                        <div class="col-md-2"><p style="padding-top: 5px;">支持xls/xlsx格式</p></div>
                        <div class="col-md-2">
                            <button class="btn btn-info" type="button" onclick="check_file()">上传</button>
                            <button class="btn btn-info" type="button" onclick="down_template()">下载模板</button>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-md-3 control-label"></label>
                        <div class="col-md-3">
                            <span id="tip"></span>
                        </div>

                    </div>
                </form>
                <div class="clearfix"></div>
                <hr>
                <div class="show_error">
                </div>

            </div>
        </div>
    </div>
    <script>
        function down_template() {
            window.location.href ='__URL__/down_template';
        }
        function check_file() {
            var strFileName=form1.file.value;
            if (strFileName=="")
            {
                alert("请选择要上传的文件");
                return false;
            }
            var form = new FormData(document.forms.namedItem("form1" ));
            $.ajax({
                url:"__URL__/Special_batch_create",
                type:"post",
                data:form,
                processData:false,
                contentType:false,
                dataType: "json",
                success:function(data){
                    if(data.data){
                        var html='';
                        $.each(data.data, function (n, result) {
                            html += '<ul>' + '<li>' + data.data[n] + '</li>' + '</ul>';
                        });
                        $(".show_error").html(html);
                        return false;
                    }
                }
            });
        }
        $(document).ready(function () {
            $("input[name='sku']").blur(function () {
                var sku = $("input[name='sku']").val();
                $.ajax({
                    type: "POST",
                    url: 'Warehouse/StockIn/get_supplier_info',
                    data: {'sku': sku},
                    dataType: "json",
                    success: function (data) {
                        if (data.data) {
                            $("input[name='sku_name']").val(data.data.sku_name);
                            $("input[name='price']").val(data.data.price);
                            var html = '';
                            $.each(data.data.supplier_id, function (n, data) {
                                html += '<option>' + data.supplier_id + '</option>';
                            });
                            if (html) {
                                $("#supplier_id").empty();
                                $("#supplier_id").append(html);
                            }
                        }
                    }

                });


            });
        });

        $("input[name=storage_position]").keydown(function (e) {
            if (e.keyCode == 13) {
                if ($("input[name=storage_position]").val() && $("input[name=sku]").val()) {
                    $.ajax({
                        type: "post",
                        url: 'Warehouse/StockIn/Judge_is_skucname',
                        data: {
                            'sku': $.trim($("input[name=sku]").val()),
                            'storage_position': $.trim($("input[name=storage_position]").val()),
                            'export_tax_rebate': $.trim($("select[name=export_tax_rebate]").val()),
                            'enterprise_dominant': $.trim($("select[name=enterprise_dominant]").val())
                        },
                        dataType: "json",
                        success: function (result) {
                            if (result.flag == false) {
                                popup.error("该sku没有绑定此储位");
                                setTimeout(function () {
                                    popup.close("asyncbox_error");
                                }, 2000);
                                return false;
                            }
                        }
                    });
                } else {
                    layer.msg('储位为空', {offset: 250});
                    return false;
                }
            }


        });
        function count_money() {
            var data = {
                'num': $.trim($("input[name='num']").val()),
                'price': $.trim($("input[name='price']").val()),
                'money': $.trim($("input[name='money']").val()),
                'cost': $.trim($("input[name='cost']").val()),
                'transporttation_expense': $.trim($("input[name='transporttation_expense']").val())
            }
            if (parseInt(data.num)) {
                $("input[name='money']").val((parseInt(data.num) * parseFloat(data.price)).toFixed(4));
                $("input[name='cost']").val(((parseInt(data.num) * parseFloat(data.price)) + parseFloat(data.transporttation_expense)).toFixed(4));
            }
        }


        //创建特殊入库单据
        $("#submit").click(function () {
            if ($("select[name=supplier_id]").val() == null || $("select[name=type]").val() == '' || $("input[name=sku]").val() == '' ||
                    $("select[name=export_tax_rebate]").val() == '' || $("input[name=sku_name]").val() == '' ||
                    $("select[name=enterprise_dominant]").val() == '' || $("input[name=storage_position]").val() == '' ||
                    $("input[name=price]").val() == '' || $("input[name=num]").val() == '' || $("input[name=money]").val() == '' ||
                    $("input[name=cost]").val() == '' || $("input[name=transporttation_expense]").val() == '' ||
                    $("input[name=op_time]").val() == '' || $("select[name=transfer_hopper_id]").val() == '' ||
                    $("select[name=transfer_type]").val() == '' || $("select[name=site_id]").val() == ''
            ) {
                layer.msg('*号输入框为必须填写', {offset: 250});
                return false;
            } else {

                $.ajax({
                    type: "post",
                    url: 'Warehouse/StockIn/Judge_is_skucname',
                    data: {
                        'sku': $.trim($("input[name=sku]").val()),
                        'storage_position': $.trim($("input[name=storage_position]").val()),
                        'export_tax_rebate': $.trim($("select[name=export_tax_rebate]").val()),
                        'enterprise_dominant': $.trim($("select[name=enterprise_dominant]").val())
                    },
                    dataType: "json",
                    success: function (result) {
                        if (result.flag == false) {
                            popup.error("该sku没有绑定此储位");
                            setTimeout(function () {
                                popup.close("asyncbox_error");
                            }, 2000);
                            return false;
                        }else{
                            $.ajax({
                                type: "post",
                                url: 'Warehouse/StockIn/Special_StockIn',
                                data: {
                                    'warehouseorders': {
                                        'type': $("select[name=type]").val(),
                                        'warehouse_quantity': $("input[name=num]").val(),
                                        'available_quantity': $("input[name=num]").val(),
                                        'sku': $("input[name=sku]").val(),
                                        'export_tax_rebate': $("select[name=export_tax_rebate]").val(),
                                        'sku_name': $("input[name=sku_name]").val(),
                                        'enterprise_dominant': $("select[name=enterprise_dominant]").val(),
                                        'storage_position': $("input[name=storage_position]").val(),
                                        'single_price': $("input[name=price]").val(),
                                        'money': $("input[name=money]").val(),
                                        'cost': $("input[name=cost]").val(),
                                        'transportation_expense': $("input[name=transporttation_expense]").val(),
                                        'op_time': $("input[name=op_time]").val(),
                                        'remark': $("input[name=remark]").val(),
                                        'supplier_id': $("select[name=supplier_id]").val(),
                                        'transfer_hopper_id': $("select[name=transfer_hopper_id]").val(),
                                        'transfer_type': $("select[name=transfer_type]").val(),
                                        'site_id': $("select[name=site_id]").val()
                                    }
                                },

                                dataType: "json",
                                success: function (result) {
                                    if (result.status == false) {
                                        popup.error("新建失败");
                                        setTimeout(function () {
                                            popup.close("asyncbox_error");
                                        }, 2000);
                                        return false;
                                    }
                                    if (result.data) {
                                        $(".show").show();
                                        var data = result.data;
                                        $(".show_detail").html('');
                                        var html = '';
                                        $.each(data, function (n, result) {
                                            html += '<tr>' + '<td>' + data[n].id + '</td>' + '<td>' + data[n].site_id + '</td>' +'<td>' + data[n].storage_position + '</td>' +
                                                    '<td>' + data[n].enterprise_dominant + '</td>' + '<td>' + data[n].transfer_hopper_id + '</td>' +
                                                    '<td>' + data[n].export_tax_rebate + '</td>' + '<td>' + data[n].purchaseorders_id + '</td>' +
                                                    '<td>' + data[n].check_quality_detail_id + '</td>' + '<td>' + data[n].op_time + '</td>' +
                                                    '<td>' + data[n].sku + '</td>' + '<td>' + data[n].sku_name + '</td>' +
                                                    '<td>' + data[n].supplier_id + '</td>' + '<td>' + data[n].warehouse_quantity + '</td>' +
                                                    '<td>' + data[n].single_price + '</td>' + '<td>' + data[n].money + '</td>' +
                                                    '<td>' + data[n].transportation_expense + '</td>' + '<td>' + data[n].cost + '</td>' +
                                                    '<td>' + data[n].type + '</td>' + '<td>' + data[n].store + '</td>' +
                                                    '<td>' + data[n].transfer_type + '</td>' + '<td>' + data[n].remark + '</td>' +
                                                    '<td>' + data[n].warehouse_man + '</td>' +
                                                    '</tr>';
                                        });
                                        $(".show_detail").html(html);
                                    }
                                }
                            });

                        }
                    }
                });

            }
        });
    </script>